package bairui.international.weixin.portal.miniapp.controller.product.product;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.model.uml.model.status.State_UpDown;
import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.model.domain.jpa.TB_ProductCategoryProperty;
import bairui.international.model.domain.jpa.TB_ProductCategoryPropertyItem;
import bairui.international.service.Service_ProductCategoryProperty;
import bairui.international.service.Service_ProductCategoryPropertyItem;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 *
 * 属性列表
 *
 * @generated
 * 
 * @see STest_C_Product_ListProperties_Get 测试
 *
 */
@Controller
public class C_Product_ListProperties_Get
 extends SC_Product_ListProperties_Get 
{
	
	@Autowired private Service_ProductCategoryProperty service_ProductCategoryProperty;
	@Autowired private Service_ProductCategoryPropertyItem service_ProductCategoryPropertyItem;
//	@Autowired private Service_ProductProperty service_ProductProperty;
//	@Autowired private Service_ProductPropertyItem service_ProductPropertyItem;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request, @RequestParam(required=false) Long categoryId
	) throws Exception {
		if(null == categoryId){
			return new Output();
		}
		Page<TB_ProductCategoryProperty> page = service_ProductCategoryProperty.page(
				it->it.productCategory.id.eq(categoryId)
					.and(it.productProperty.state.eq(State_UpDown.Uped)), 
				PageRequest.of(0, 3),
				it->new QSort(it.orderValue.desc())
			);
//		Page<TB_ProductProperty> page = service_ProductProperty.page(
//				it->it.state.eq(State_UpDown.Uped).and(it.productCategoryProperties.any().productCategory.id.eq(categoryId)), 
//				PageRequest.of(0, 3),
//				it->new QSort(it.orderValue.desc())
//			);
		return page
				.map(it->{
					List<TB_ProductCategoryPropertyItem> item = service_ProductCategoryPropertyItem
							.list(
								it2->it2.productCategoryProperty.eq(it)
									.and(it2.productPropertyItem.state.eq(State_UpDown.Uped)),
								it2->new QSort(it2.orderValue.desc())
							).getContent();
					return new Output(
							it.getProductProperty().getId(), 
							it.getProductProperty().getName(), 
							item
								.stream()
								.map(it2->new OutputItem(it2.getProductPropertyItem().getId(), it2.getProductPropertyItem().getName()))
								.collect(Collectors.toList())
						);
				})
				.getContent();
	}
	
	@NoArgsConstructor
	@AllArgsConstructor
	private static class Output { 
		@Getter private Long id;
		@Getter private String name;
		@Getter private List<OutputItem> items;
	}
	
	@AllArgsConstructor
	private static class OutputItem { 
		@Getter private Long id;
		@Getter private String name;
	}
	
}
